//
//  LisService.m
//  Nurse
//
//  Created by 夏 申频 on 14-2-16.
//  Copyright (c) 2014年 Honeywell. All rights reserved.
//

#import "LisService.h"

NSString* const PROCEDUREREPORT = @"DY_OUT_HYSQ";
NSString* const PROCEDUREDETAIL = @"DY_OUT_HYJG";

@interface LisService()
@property (nonatomic,strong) NSURL* url;
@end


@implementation LisService

-(LisService*)init{
    self= [super init];
    if (self) {
        self.url = [[DataEngine sharedInstance]urlForPath:@"/his/procedure"];
    }
    return self;
}

+(LisService*) sharedInstance{
    static dispatch_once_t pred = 0;
    __strong static id _sharedObject = nil;
    dispatch_once(&pred, ^{
        _sharedObject = [[LisService alloc] init];
    });
    return _sharedObject;
}

-(void)queryLisReportForPID:(NSString*)pid withSuccessHandler:(SuccessHandler)successHandler andFailHandler:(FailHandler)failHandler{
    ProcedureCallRequest* request = [[ProcedureCallRequest alloc]init];
    request.procedureName = PROCEDUREREPORT;
    request.params = @{@"病人编号":pid};
    request.failHandle = failHandler;
    request.successHandle = ^(id result){
        if ([result isKindOfClass:[NSArray class]]) {
            if ([result count]>0) {
                //id record = [result objectAtIndex:0];
                if (successHandler) {
                    successHandler(result);
                }
            }else{
                NSError* error = [DataEngine makeErrorForMsg:@"该病人没有检验记录"];
                if (failHandler) {
                    failHandler(error,@"没有记录");
                }
                
            }
        }else{
            if (failHandler) {
                NSError* error = [DataEngine makeErrorForMsg:@"从服务器获得错误的返类型，期望获得NSArray"];
                failHandler(error,@"类型错误");
            }
        }
    };
    [[DataEngine sharedInstance]callProcedure:request forURL:self.url];
}
-(void)queryLisReportDetailsForID:(NSString*)_id withSuccessHandler:(SuccessHandler)successHandler andFailHandler:(FailHandler)failHandler{
    ProcedureCallRequest* request = [[ProcedureCallRequest alloc]init];
    request.procedureName = PROCEDUREDETAIL;
    request.params = @{@"申请编号":_id};
    request.failHandle = failHandler;
    request.successHandle = ^(id result){
        if ([result isKindOfClass:[NSArray class]]) {
            if ([result count]>0) {
                //id record = [result objectAtIndex:0];
                if (successHandler) {
                    successHandler(result);
                }
            }else{
                NSError* error = [DataEngine makeErrorForMsg:@"本次记录没有结果"];
                if (failHandler) {
                    failHandler(error,@"没有结果");
                }
                
            }
        }else{
            if (failHandler) {
                NSError* error = [DataEngine makeErrorForMsg:@"从服务器获得错误的返类型，期望获得NSArray"];
                failHandler(error,@"类型错误");
            }
        }
    };
    [[DataEngine sharedInstance]callProcedure:request forURL:self.url];

}

@end
